package UseStream;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class StreamMiddleMethod {
    public static void main(String[] args) {
        //useFilter();
        //useLimitSkip();
        //useDistinct();
        // useMap();
        useConcat();

    }
    // filter函数的使用
    public static void useFilter(){
        ArrayList<String> list = new ArrayList<>();
        Collections.addAll(list,"小同","小平","大狗");
        // 过滤小开头的字符串
        List<String> small = list.stream().filter(s -> s.startsWith("小")).collect(Collectors.toList());
        System.out.println(small);
    }
    // limit和skip的使用
    public static void useLimitSkip(){
        ArrayList<String> list = new ArrayList<>();
        Collections.addAll(list,"小同","小平","大狗");
        List<String> list1 = list.stream().limit(2).toList();
        System.out.println(list1);
        List<String> list2 = list.stream().skip(1).toList();
        System.out.println(list2);
    }
    // distinct去重的使用
    public static void useDistinct(){
        // distinct去除重复的元素，存储的元素必须要实现hashcode与equals
        ArrayList<String> list = new ArrayList<>();
        Collections.addAll(list,"小同","小平","大狗","小同");
        List<String> list1 = list.stream().distinct().toList();
        System.out.println(list1);
    }
    // map转换数据类型操作
    public static void useMap(){
        ArrayList<String> list = new ArrayList<>();
        Collections.addAll(list,"小同-18","小平-19","大狗-20");
        List<Integer> list1 = list.stream().map(new Function<String, Integer>() {
            @Override
            public Integer apply(String s) {
                String age = s.split("-")[1];
                return Integer.parseInt(age);
            }
        }).toList();
        // System.out.println(list1);
        // lambada表达式操作
        List<Integer> list2 = list.stream().map(s -> {
            String age = s.split("-")[1];
            return Integer.parseInt(age);
        }).toList();
        System.out.println(list2);
    }
    // 合并流使用Stream工具类中的方法
    public static void useConcat(){
        ArrayList<String> list = new ArrayList<>();
        Collections.addAll(list,"小丹-18","小平-19","小同-20");
        ArrayList<String> list1 = new ArrayList<>();
        Collections.addAll(list,"大长-21","大水-22","大小-23");
        List<String> concat = Stream.concat(list.stream(), list1.stream())
                .filter(s -> s.startsWith("大")).toList();
        System.out.println(concat);

    }

}
